package com.myapp.ruyixing.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.myapp.ruyixing.R;

public class AirAndTrainSearchActivity extends BaseActivity implements
		OnClickListener {

	TextView mTitleView;
	View mShadowsView;
	View mMenuLayout;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		
		setContentView(R.layout.activity_air_and_trainsearch);

		mTitleView = (TextView) findViewById(R.id.text_title);
		findViewById(R.id.img_icon).setOnClickListener(this);
		mTitleView.setOnClickListener(this);
		mShadowsView = findViewById(R.id.shadows);
		mMenuLayout = findViewById(R.id.menus);

		if (arg0 == null) {
			showAirForm();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_icon:
			finish();
			break;
		case R.id.text_title:
			showMenu();
			break;

		default:
			break;
		}

	}

	public void changeType(String value, String name) {
		mTitleView.setText(name);
		hideMenu();

		if ("机票".equals(name)) {
			showAirForm();
		} else if ("火车票".equals(name)) {
			showTrainForm();
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
	}

	private void showMenu() {
		mMenuLayout.setVisibility(View.VISIBLE);
		mShadowsView.setVisibility(View.VISIBLE);
	}

	private void hideMenu() {
		mMenuLayout.setVisibility(View.GONE);
		mShadowsView.setVisibility(View.GONE);
	}

	private void showAirForm() {
		Fragment current = getSupportFragmentManager().findFragmentById(
				R.id.container);
		if (!(current instanceof AirTicketSearchFragment)) {
			getSupportFragmentManager().beginTransaction().replace(
					R.id.container, new AirTicketSearchFragment()).commit();
		}
	}

	private void showTrainForm() {
		Fragment current = getSupportFragmentManager().findFragmentById(
				R.id.container);
		if (!(current instanceof TrainTicketSearchFragment)) {
			getSupportFragmentManager().beginTransaction().replace(
					R.id.container, new TrainTicketSearchFragment()).commit();
		}
	}
}
